using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace GeometryViz3D
{
    public class MRUSection : ConfigurationSection
    {
        public MRUSection()
        {
        }

        [ConfigurationProperty("Models", DefaultValue = "")]
        public string Models
        {
            get
            {
                return (string)this["Models"];
            }

            set
            {
                this["Models"] = value;
            }
        }

        public static MRUSection GetSection(Configuration config)
        {
            return (MRUSection)config.Sections["MRUSection"];
        }

        public string[] GetModels()
        {
            return SplitString(this.Models);
        }

        private string[] SplitString(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }

            return str.Split(';');
        }

        public void OnOpenModel(string file)
        {
            this.Models = AddNewItem(this.Models, file);
        }

        private string AddNewItem(string list, string item)
        {
            if (string.IsNullOrEmpty(list))
            {
                return item;
            }
            else
            {
                string[] items = SplitString(list);

                if (items.Length == 0)
                {
                    return "";
                }

                StringBuilder sb = new StringBuilder();
                sb.Append(item);

                for (int i = 0; i < items.Length; i++)
                {
                    if (string.Compare(items[i], item, true) != 0)
                    {
                        sb.Append(";");
                        sb.Append(items[i]);
                    }
                }

                return sb.ToString();
            }
        }
    }
}
